-module(cust_service).
-behaviour(gen_server).
-include("rec_dec.hrl").
-include_lib("stdlib/include/qlc.hrl").

-export([start/0,init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3,start/1,check/1]).
-export([addchange/1,get/0,get/1,dump/0]).
-define(SERVER, ?MODULE).
-define(TAB    , cust_info).

start() ->
	ok = start(db),
    case gen_server:start({local, ?SERVER}, ?MODULE, [], []) of 
	{ok,_} ->
		ok;
	{error,Reason} ->
		%%stop(),
		io:format("start cust service failure with ~p~n",[Reason])
	end.
dump() ->
	io:format("~p",[record_info(fields,?TAB)]).
	
start(db) ->
	case db_mgr:check_table(?TAB) of
	no_found ->
		error_logger:warning_msg("create db table for (~s) with result:~p",
					[?MODULE,
					db_mgr:create_table(?TAB,record_info(fields,?TAB))]),
		ok;
	_ ->
		ok
	end.
	
init(_) ->
	error_logger:info_msg("cust service is started~n"),
    {ok,[]}.

handle_call({add,{"",_,_,_,_}},_,Custs) ->
	{reply,ok,Custs};

handle_call({check,Mobile},_,Custs) ->
	{reply,
		case lists:keyfind(Mobile,1,Custs) of
    	false ->
    		[];
    	{Mobile,_,_} ->
    		{undefined,Mobile}
    	end,
     Custs
     };

handle_call({add,{Mobile,Name,V_Id,Q_Id,Index}},_,Custs) ->
    case lists:keyfind(Mobile,1,Custs) of
    false ->
		Cust = addchange({Mobile,Name}),
        {reply,{ok,Cust#cust_info.id},
		[{Mobile,{Name,V_Id,Q_Id,Index}, reserve}
		| Custs]};  
    {Mobile,{N,V,Q,I},reserve} ->
		{reply,{in_use,{N,V,Q,I}},Custs}
    end;

	
handle_call({del,{_,Mobile,_,_,_}},_,Custs) ->
    {reply,ok,lists:keydelete(Mobile,1,Custs)};


handle_call({send_sms,{Cust_Id,Evt,Evt_Id,Content}},_,Custs) ->
	case cust_service:get({id,Cust_Id}) of
	[] ->
		{reply,{error,no_cust_info},Custs};
	[Cust] ->
%% do sms sending
		sms:send(post,Cust#cust_info.mobile,Content),
		{reply,ok,Custs}
	end;
	
handle_call(stop,_,{Q,Index,Custs}) ->
	{stop,"By request",ok,{Q,Index,Custs}};

handle_call(dump,_,Custs) ->
	io:format("~p~n",[Custs]),
	{reply,ok,Custs};
	
handle_call(_,_,State) ->
	{reply, undefined, State}.	
handle_cast(_, State) ->
	{noreply,State}.
handle_info(_, State) ->
	{noreply,State}.
terminate(_, _) ->
	ok.
code_change(_, State, _) ->
	{ok,State}.

check({mobile,Mobile}) ->
	case gen_server:call(cust_service,{check,list_to_binary(Mobile)}) of
	{undefined,_} ->
		{undefined,Mobile};
	[] ->
		cust_service:get({mobile,Mobile})
	end.

addchange({Mobile,Name}) ->	
	case cust_service:get({mobile,Mobile}) of
	[] ->
		addchange(#cust_info{name = Name,mobile=Mobile,id=""});
	[Cust] ->
		addchange(Cust#cust_info{name = Name});
	_ ->
		#cust_info{name = Name,mobile=Mobile,id=""}
	end;

	
addchange(Rec) when is_record(Rec,?TAB) ->
	case Rec#?TAB.id of
	"" ->
		add(Rec);
	_ ->
		modify(Rec)
	end.


add(Rec) when is_record(Rec,?TAB) ->
	Id = gen_server:call(couch_uuids,create),
	db_mgr:write(?TAB,Rec#?TAB{id=Id}).
	

modify(Rec) when is_record(Rec,?TAB) ->
	db_mgr:write(?TAB,Rec).
	
get() -> %% get all vendor info
	db_mgr:get(?TAB). 
	
get({id,Id}) ->
	Qry = qlc:q([X||X <- mnesia:table(?TAB),X#?TAB.id =:= Id]),
	db_mgr:do_transaction(fun() -> qlc:e(Qry) end);
	
get({mobile,Mobile}) -> 
	Qry = qlc:q([X||X <- mnesia:table(?TAB),X#?TAB.mobile =:= Mobile]),
	db_mgr:do_transaction(fun() -> qlc:e(Qry) end).
	